home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1995 May / cd Ware (Juegos) Epimundo.iso / DOS / PRGMMING / CPP100.ZIP / CPPFIC4.C < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-22  |  19.0 KB  |  527 lines

  1. /*****************************************************************************/
  2. /*       (C) 1993,1994 R. NADE - M. GRANDCHAMP - All Rights Reserved         */
  3. /*****************************************************************************/
  4. /*    This source-code is NOT public domain nor Freeware, this is part of    */
  5. /*              'The C Programming Package' which is Shareware.              */
  6. /*   If you use this code, please register and get a free Full-VGA version   */
  7. /*****************************************************************************/
  8.                             /*-----------------------*
  9.                              *       CPPFIC4.C       *
  10.                              *     File Manager      *
  11.                              *  Display of selected  *
  12.                              *    file (CPPFIC1B)    *
  13.                              *    store in filedir.  *
  14.                              *-----------------------*/
  15.  
  16.  /* Included Files */
  17.  # include <stdio.h>
  18.  # include <io.h>
  19.  # include <dos.h>
  20.  # include <fcntl.h>
  21.  # include <bios.h>
  22.  # include <stdlib.h>
  23.  # include <conio.h>
  24.  # include <dir.h>
  25.  # include <string.h>
  26.  # include <alloc.h>
  27.  # include <mem.h>
  28.  # include <sys\stat.h>
  29.  
  30.  /* Global Variables */
  31.  extern int bm,i,j,k,l,m,n,t,x,xm,y,ym,z,menu0,menu1,return1,hexa,line;
  32.  extern int colour1,colour2,colour3,colour4,colour5,nbdisk,return2,return3,recording;
  33.  extern int lline,linemax,chx,choice,modif,pfdiv,pfserv,pfhelp,pfic,returning;
  34.  extern unsigned int ref,nbref,nbrep;
  35.  extern long dep,depdiv,dephelp,depbeginning,enddep,depfile;
  36.  extern unsigned long nbbytes;
  37.  extern char *memory,*diversity;
  38.  extern unsigned char c0,c1,trashcan[],path[],nc1[],trashret[],filedir[];
  39.  extern unsigned char work[],record[],saverep[];
  40.  extern struct ffblk ffblk;
  41.  extern struct directory{
  42.                        unsigned char name[9];
  43.                        unsigned char ext[4];
  44.                        unsigned char size[9];
  45.                        unsigned char attribut[2];
  46.                        unsigned char date[9];
  47.                        unsigned char time[6];
  48.                      } directory;
  49.  
  50.  /*------------------------------*
  51.   * CLEAR SPACES (variable char) *
  52.   *------------------------------*/
  53.   void clear_spaces(varchar)
  54.     unsigned char *varchar;
  55.     {
  56.       /* Cut spaces on the right of the word */
  57.       i=strlen(varchar);
  58.       for(j=(i-1); j>0; j--){
  59.     if((varchar[j]==0x00)||(varchar[j]==0x20))   varchar[j]=0x00;
  60.     else                                         break;
  61.       }
  62.       /* Then, if required, on the left */
  63.       i=strlen(varchar);
  64.       for(j=0; j<i; j++)           if(varchar[j]!=0x20) break;
  65.       for(k=j; k<i; k++)               trashret[k-j]=varchar[k];
  66.       trashret[i-j]=0x00;
  67.     }
  68.  /*------------------------------*
  69.   *      DEFINE DISPLAY MODE     *
  70.   *         Hexa or ASCII        *
  71.   * for the file to be displayed *
  72.   *------------------------------*/
  73.   void define_display_mode()
  74.     {
  75.       hexa=1;
  76.       strcpy(trashcan,directory.ext);     clear_spaces(trashcan);
  77.       /* If there is no extension or not an extension usually used */
  78.       /* to indicate a text file, we choose "a priori"             */
  79.       /* since it can be changed in the menu if it is not correct  */
  80.       if(strlen(trashcan)==0)          hexa=0;
  81.       if(strcmp(trashcan,"TXT")==0)    hexa=0;
  82.       if(strcmp(trashcan,"DOC")==0)    hexa=0;
  83.       if(strcmp(trashcan,"1ST")==0)    hexa=0;
  84.       if(strcmp(trashcan,"ME")==0)     hexa=0;
  85.       if(strcmp(trashcan,"ASC")==0)    hexa=0;
  86.       if(strcmp(trashcan,"MOI")==0)    hexa=0;
  87.       if(strcmp(trashcan,"PRG")==0)    hexa=0;
  88.       if(strcmp(trashcan,"C")==0)      hexa=0;
  89.       if(strcmp(trashcan,"PAS")==0)    hexa=0;
  90.       if(strcmp(trashcan,"   ")==0)    hexa=0;
  91.     }
  92.  /*--------------------------------*
  93.   * OPEN SELECTED FILE FOR READING *
  94.   *--------------------------------*/
  95.   int open_selected_file_for_reading()
  96.     {
  97.       if(pfic>0)                       close(pfic);
  98.       pfic=open(filedir,O_RDONLY | O_BINARY,S_IREAD);
  99.       if(pfic==-1){
  100.         warning(" Cannot open"," the selected file ",""," Click the mouse"," or press any key");
  101.     return(0);
  102.       }
  103.       enddep=lseek(pfic,0L,SEEK_END);   depfile=lseek(pfic,0L,SEEK_SET);
  104.       depbeginning=depfile;                  lseek(pfic,depfile,0);
  105.       return(1);
  106.     }
  107.  /*------------------------------*
  108.   * DISPLAY BORDER FOR HEXA FILE *
  109.   *------------------------------*/
  110.   void display_border_hexa_file()
  111.     {
  112.       /* Display in hexa with ASCII translation if possible */
  113.       textattr(colour3);
  114.       gotoxy(22,3);    cputs("╔══════════╤════════════════════════╤════════╗");
  115.       gotoxy(22,4);    cputs("║  Offset  │    Hexadecimal Text    │  Text  ║");
  116.       gotoxy(22,5);    cputs("╟──────────┼────────────────────────┼────────╢");
  117.       for(i=6; i<24; i++){
  118.     gotoxy(22,i);  cputs("║          │                        │        ║");
  119.       }
  120.       gotoxy(22,24);   cputs("╩══════════╧════════════════════════╧════════╝");
  121.     }
  122.  /*----------------------------*
  123.   * DISPLAY HEXA FILE - 1 PAGE *
  124.   *----------------------------*/
  125.   void display_hexa_file()
  126.     {
  127.       /* Display the border and erase the old one if required */
  128.       display_border_hexa_file();
  129.       for(line=6; line<24; line++){
  130.         /* Display the offset from the beginning of the file */
  131.         ltoa(depfile,nc1,10);          clear_spaces(nc1);
  132.         strcpy(trashcan,trashret);
  133.         put_separators();              m=strlen(trashcan);
  134.         gotoxy((32-m),line);           cputs(trashcan);
  135.         trashcan[8]=0x00;
  136.         /* Then the text itself, if printable */
  137.         if(depfile>(enddep-8)){
  138.           /* This is the end of the file */
  139.           k=enddep-depfile;             trashcan[k]=0x00;
  140.           lseek(pfic,depfile,0);        read(pfic,trashcan,k);
  141.       for(i=0; i<k; i++){
  142.             j=trashcan[i];              itoa(j,nc1,16);
  143.       if(j<0x10){
  144.         nc1[1]=nc1[0];             nc1[0]=0x30;
  145.       }
  146.       nc1[2]=0x00;
  147.       gotoxy((3*i+34),line);       cputs(nc1);
  148.       }
  149.       for(j=0; j<k; j++){
  150.             /* Cut all the control chars */
  151.             if(trashcan[j]<0x21)       trashcan[j]=0xfa;
  152.       }
  153.           gotoxy(59,line);             cputs(trashcan);
  154.       break;
  155.     }
  156.         /* Else read the whole line */
  157.         lseek(pfic,depfile,0);          read(pfic,trashcan,8);
  158.         /* Display hexa as is */
  159.     for(i=0; i<8; i++){
  160.           j=trashcan[i];               itoa(j,nc1,16);
  161.       if(j<0x10){
  162.         nc1[1]=nc1[0];             nc1[0]=0x30;
  163.       }
  164.       nc1[2]=0x00;
  165.       gotoxy((3*i+34),line);       cputs(nc1);
  166.     }
  167.     for(j=0; j<8; j++){
  168.           /* Treat every line but cut all the control chars */
  169.           if(trashcan[j]<0x21)         trashcan[j]=0xfa;
  170.     }
  171.         /* Then display the line */
  172.         gotoxy(59,line);               cputs(trashcan);
  173.         /* And then go to the next line */
  174.         depfile+=8;
  175.         if(depfile>=enddep){
  176.           /* This is the end of the file */
  177.           sound1();                      break;
  178.     }
  179.       }
  180.     }
  181.  /*---------------------------*
  182.   * DISPLAY BORDER ASCII FILE *
  183.   *---------------------------*/
  184.   void display_border_ascii_file()
  185.     {
  186.       /* displayed in ASCII if possible */
  187.       textattr(colour3);
  188.       gotoxy(22,3);    cputs("╔══════════╤═════════════════════════════════╗");
  189.       gotoxy(22,4);    cputs("║  Offset  │     ASCII text (if printable)   ║");
  190.       gotoxy(22,5);    cputs("╟──────────┼─────────────────────────────────╢");
  191.       for(i=6; i<24; i++){
  192.     gotoxy(22,i);  cputs("║          │                                 ║");
  193.       }
  194.       gotoxy(22,24);   cputs("╩══════════╧═════════════════════════════════╝");
  195.     }
  196.  /*--------------------*
  197.   * DISPLAY ASCII PAGE *
  198.   *--------------------*/
  199.   void display_ascii_page()
  200.     {
  201.       /* Display the border and erase the old one if required*/
  202.       display_border_ascii_file();
  203.       for(line=6; line<24; line++){
  204.        /*  Display the offset from the beginning of the file */
  205.         ltoa(depfile,nc1,10);           clear_spaces(nc1);
  206.         strcpy(trashcan,trashret);
  207.         put_separators();         m=strlen(trashcan);
  208.         gotoxy((32-m),line);           cputs(trashcan);
  209.         trashcan[32]=0x00;
  210.         /* Then the text itself if printable */
  211.         if(depfile>(enddep-32)){
  212.           /* This is the end of the file */
  213.           k=enddep-depfile;             trashcan[k]=0x00;
  214.           lseek(pfic,depfile,0);        read(pfic,trashcan,k);
  215.       for(j=0; j<32; j++){
  216.             /* Cut all the control chars */
  217.             if(trashcan[j]<0x21)       trashcan[j]=0xfa;
  218.       }
  219.           gotoxy(35,line);             cputs(trashcan);
  220.       break;
  221.     }
  222.         /* Else read the full line */
  223.         lseek(pfic,depfile,0);          read(pfic,trashcan,32);
  224.     for(j=0; j<32; j++){
  225.            /* Treat every line but cut all the control chars */
  226.           if(trashcan[j]<0x21)         trashcan[j]=0xfa;
  227.     }
  228.         /* Then display the line */
  229.         gotoxy(35,line);               cputs(trashcan);
  230.         /* Then go to next line */
  231.         depfile+=32;
  232.         if(depfile>=enddep){
  233.           /* End of the file */
  234.           sound1();                      break;
  235.     }
  236.       }
  237.     }
  238.  /*----------------------------------*
  239.   * DISPLAY SELECTED FILE MENU *
  240.   *----------------------------------*/
  241.   void display_selected_file_menu(value)
  242.     int value;
  243.     {
  244.       hide_the_mouse();               textattr(colour3);
  245.       /* Display the modifying part of the menu */
  246.       gotoxy(69,10);                   cputs("║Beg. File ║");
  247.       gotoxy(69,11);                   cputs("║Prev. page║");
  248.       gotoxy(69,12);                   cputs("║Next page ║");
  249.       gotoxy(69,13);                   cputs("║End file  ║");
  250.       gotoxy(69,14);                   cputs("║disp. Asci║");
  251.       gotoxy(69,15);                   cputs("║disp. Hexa║");
  252.       /* Overline the key letters */
  253.       textattr(116);
  254.       gotoxy(70,10);                   cputs("B");
  255.       gotoxy(70,11);                   cputs("P");
  256.       gotoxy(70,12);                   cputs("N");
  257.       gotoxy(70,13);                   cputs("E");
  258.       gotoxy(76,14);                   cputs("A");
  259.       gotoxy(76,15);                   cputs("H");
  260.       gotoxy(71,17);                   cputs("Q");
  261.       /* Inverse the selected line */
  262.       textattr(colour5);
  263.       if(value==1){
  264.     gotoxy(70,10);                 cputs("Beg. file ");
  265.       }
  266.       if(value==2){
  267.     gotoxy(70,11);                 cputs("Prev. page");
  268.       }
  269.       if(value==3){
  270.     gotoxy(70,12);                 cputs("Next page ");
  271.       }
  272.       if(value==4){
  273.     gotoxy(70,13);                 cputs("End file  ");
  274.       }
  275.       if(hexa==0){
  276.     gotoxy(70,14);                 cputs("disp. Asci");
  277.       }
  278.       if(hexa==1){
  279.     gotoxy(70,15);                 cputs("disp. Hexa");
  280.       }
  281.       show_the_mouse();
  282.     }
  283.  /*---------------------*
  284.   * DISPLAY FILE BORDER *
  285.   *---------------------*/
  286.   void display_file_border()
  287.     {
  288.       hide_the_mouse();               textattr(colour1);
  289.       gotoxy(69,5);                    cputs("            ");
  290.       textattr(colour3);
  291.       /* Erase what we do not need */
  292.       window(22,3,67,24);              clrscr();
  293.       window(69,6,80,24);              clrscr();
  294.       window(1,18,22,24);              clrscr();
  295.       window(1,1,80,25);
  296.       /* Get files info. */
  297.       gotoxy(1,18);                    cputs("╔════════════════════╣");
  298.       gotoxy(1,19);                    cputs("║Drive =             ");
  299.       gotoxy(1,20);                    cputs("║Files =             ");
  300.       gotoxy(1,21);                    cputs("║Size  =         Byt.");
  301.       gotoxy(1,22);                    cputs("║Date  =             ");
  302.       gotoxy(1,23);                    cputs("║Time  =             ");
  303.       gotoxy(1,24);                    cputs("╚════════════════════");
  304.       memcpy(trashcan,filedir,3);      trashcan[3]=0x00;
  305.       gotoxy(18,19);                   cputs(trashcan);
  306.       strcpy(trashcan,directory.name); clear_spaces(trashcan);
  307.       strcpy(trashcan,trashret);
  308.       strcat(trashcan,".");            strcat(trashcan,directory.ext);
  309.       clear_spaces(trashcan);          i=strlen(trashret);
  310.       gotoxy((21-i),20);               cputs(trashret);
  311.       strcpy(trashcan,directory.size); clear_spaces(trashcan);
  312.       strcpy(trashcan,trashret);
  313.       put_separators();                i=strlen(trashcan);
  314.       gotoxy((17-i),21);               cputs(trashcan);
  315.       strcpy(trashcan,directory.date); clear_spaces(trashcan);
  316.       gotoxy(13,22);                   cputs(trashret);
  317.       strcpy(trashcan,directory.time); clear_spaces(trashcan);
  318.       gotoxy(16,23);                   cputs(trashret);
  319.       /* Draw the menu for the files search */
  320.       gotoxy(69,6);                    cputs("╔══════════╗");
  321.       gotoxy(69,7);                    cputs("║  M E N U ║");
  322.       gotoxy(69,8);                    cputs("╚══════════╝");
  323.       gotoxy(69,9);                    cputs("╔══════════╗");
  324.       gotoxy(69,16);                   cputs("╟──────────╢");
  325.       gotoxy(69,17);                   cputs("║   Quit   ║");
  326.       gotoxy(69,18);                   cputs("╚══════════╝");
  327.       display_selected_file_menu(1);
  328.       show_the_mouse();
  329.     }
  330.  /*----------------------------------*
  331.   * GET_MOUSE_SELECTED_FILES_MANAGER *
  332.   *----------------------------------*/
  333.   int get_mouse_selected_files_manager()
  334.     {
  335.       if((xm>68)&&(xm<80)){
  336.     if(ym==10){
  337.       chx=1;                       return(1);
  338.     }
  339.     if(ym==11){
  340.       chx=2;                       return(1);
  341.     }
  342.     if(ym==12){
  343.       chx=3;                       return(1);
  344.     }
  345.     if(ym==13){
  346.       chx=4;                       return(1);
  347.     }
  348.     if(ym==14){
  349.       hexa=0;                      return(2);
  350.     }
  351.     if(ym==15){
  352.       hexa=1;                      return(2);
  353.     }
  354.     if(ym==17){
  355.       chx=7;                       return(-1);
  356.     }
  357.       }
  358.       return(0);
  359.     }
  360.  /*--------------------------------*
  361.   * DISPLAY SELECTED FILES MANAGER *
  362.   *--------------------------------*/
  363.   int display_selected_files_manager()
  364.     {
  365.       t=wait_for_a_key();
  366.       /* If we pressed <Esc> <Q> <q> we go back to the menu */
  367.       if((t==-1)||(t==26))           return(-1);
  368.       if((t==13)||(t==71)){
  369.         /* Beginning of the file */
  370.     chx=1;                           return(1);
  371.       }
  372.       if((t==25)||(t==72)||(t==73)||(t==75)){
  373.         /* Previous page */
  374.     chx=2;                           return(1);
  375.       }
  376.       if((t==23)||(t==80)||(t==81)||(t==77)){
  377.         /* Next page */
  378.     chx=3;                           return(1);
  379.       }
  380.       if((t==14)||(t==79)){
  381.         /* End of the file */
  382.     chx=4;                           return(1);
  383.       }
  384.       if(t==10){
  385.         /* Display in ASCII */
  386.     hexa=0;                           return(2);
  387.       }
  388.       if(t==17){
  389.         /* Display in Hexa */
  390.     hexa=1;                           return(2);
  391.       }
  392.       if(t==59){
  393.         /* <F1> = Help */
  394.     return(59);
  395.       }
  396.       if(t==100){
  397.         /* We used the mouse */
  398.         t=get_mouse_selected_files_manager();
  399.     return(t);
  400.       }
  401.      /* If we choosed something else or if we clicked */
  402.      /* outside of the defined zone, nothing happens */
  403.       return(0);
  404.     }
  405.  /*----------------------------*
  406.   *    DISPLAY SELECTED FILE   *
  407.   * the full name is in filedir *
  408.   *----------------------------*/
  409.   void display_selected_file()
  410.     {
  411.       /* Open the file in read only mode */
  412.       returning=open_selected_file_for_reading();
  413.       if(returning<1)                     return;
  414.       define_display_mode();     display_file_border();
  415.       while(1){
  416.         if(hexa==0)                    display_ascii_page();
  417.         else                           display_hexa_file();
  418.         return3=display_selected_files_manager();
  419.         if(return3==-1)                break;
  420.         if(return3==59){
  421.           /* On-line Help */
  422.           display_help("FILES",9,6,1);     restore_help_screen();
  423.     }
  424.         if(return3==1){
  425.           display_selected_file_menu(chx);
  426.       if(chx==1){
  427.             /* Beginning of the file */
  428.             depfile=depbeginning;           sound1();
  429.       }
  430.       if(chx==2){
  431.             /* previous page of the file          */
  432.             /* We need to go up twice in the file */
  433.             /* once for the displayed page,       */
  434.             /* once for the previous page         */
  435.         if(hexa==0){
  436.               /* ASCII  */
  437.               depfile-=1152;
  438.               if(depfile<=depbeginning){
  439.                 /* beginning of the file */
  440.                 depfile=depbeginning;       sound1();
  441.           }
  442.         }
  443.         else{
  444.               /* hexadecimal */
  445.               depfile-=288;
  446.               if(depfile<=depbeginning){
  447.                 /* Beginning of the file */
  448.                 depfile=depbeginning;       sound1();
  449.           }
  450.         }
  451.       }
  452.       if(chx==3){
  453.             /* Next page of the file */
  454.             /* We let the file reading going on */
  455.       }
  456.       if(chx==4){
  457.             /* End of the file */
  458.         if(hexa==0){
  459.               /* ASCII */
  460.               depfile=enddep-576;       sound1();
  461.               if(depfile<=depbeginning){
  462.                 /* beginning of the file */
  463.                 depfile=depbeginning;       sound1();
  464.           }
  465.         }
  466.         else{
  467.               /* hexadecimal */
  468.               depfile=enddep-144;       sound1();
  469.               if(depfile<=depbeginning){
  470.                 /* Beginning of the file */
  471.                 depfile=depbeginning;       sound1();
  472.           }
  473.         }
  474.       }
  475.       if(chx==7){
  476.         /* Quit */
  477.         break;
  478.       }
  479.     }
  480.         if(return3==2){
  481.           display_selected_file_menu(chx);
  482.       if(hexa==0){
  483.             /* display in ASCII */
  484.             /* First we go to the beginning of the page */
  485.             depfile-=144;
  486.             if(depfile<=depbeginning){
  487.               /* Beginning of the file */
  488.               depfile=depbeginning;       sound1();
  489.         }
  490.             if(depfile>(enddep-576)){
  491.               depfile=enddep-576;       sound1();
  492.               if(depfile<=depbeginning){
  493.                 /* Beginning of the file */
  494.                 depfile=depbeginning;       sound1();
  495.           }
  496.         }
  497.       }
  498.       if(hexa==1){
  499.             /* display in Hexa */
  500.             /* We go to the beginning of the page */
  501.             depfile-=576;
  502.             if(depfile<=depbeginning){
  503.               /* Beginning of the file */
  504.               depfile=depbeginning;       sound1();
  505.         }
  506.             if(depfile>(enddep-128)){
  507.               depfile=enddep-128;       sound1();
  508.               if(depfile<=depbeginning){
  509.                 /* Beginning of the file */
  510.                 depfile=depbeginning;       sound1();
  511.           }
  512.         }
  513.       }
  514.     }
  515.       }
  516.       close(pfic);                     pfic=0;
  517.       /* Previous screen */
  518.       display_directories_listing();
  519.       diversity=memory;               calculate_and_display_sum_of_files();
  520.       memory=diversity;               display_directory_files();
  521.       display_drive_choice();
  522.       display_menu_file_manager(choice);
  523.     }
  524. /* End of the Module *//*-------------------*/
  525.  
  526.  
  527.